<template>
	<view>
		<view class="picker-mask" @click="closePicker" catchtouchmove="true" v-show="pickerShow"></view>
		<view class="picker-content" :class="{'pickerShow':pickerShow}">
			<view class="picker-button">
				<text @click="closePicker">取消</text>
				<text @click="confirm">确定</text>
			</view>
			<!-- 单列选择 -->
			<picker-view class="picker-view" indicator-class="picker-view-selected-one" :value="defaultValue" @change="pickerViewChangeOne"
			 v-if="mode=='one'">
				<picker-view-column>
					<!--  #ifdef MP-WEIXIN -->
					<view class="picker-item1" v-for="(item, index) in pickerList" :key="index">{{item.name}}</view>
					<!--  #endif -->
					<!--  #ifdef APP-PLUS -->
					<view class="picker-item1" v-for="(item, index) in pickerList" :key="index"> <text v-html="item.name"></text></view>
					<!--  #endif -->					
				</picker-view-column>
			</picker-view>
			<!-- 双列选择 -->
			<!-- 单列选择 -->
			<picker-view class="picker-view" indicator-style="height: 36px;" indicator-class="picker-view-selected-two" :value="defaultValue" @change="pickerViewChangeTwo"
			 v-if="mode=='two'">
				<picker-view-column>
					<!--  #ifdef MP-WEIXIN -->
					<view class="picker-item1" style="height: 36px;" v-for="(item, index) in pickerList" :key="index">{{item.name}}</view>
					<!--  #endif -->
					<!--  #ifdef APP-PLUS -->
					<view class="picker-item1" style="height: 36px;" v-for="(item, index) in pickerList" :key="index"><text v-html="item.name"></text></view>
					<!--  #endif -->
				</picker-view-column>
				<picker-view-column>
					<!--  #ifdef MP-WEIXIN -->
					<view class="picker-item1" style="height: 36px;" v-for="(item, index) in (pickerList[twoIndex[0]] && pickerList[twoIndex[0]].children)" :key="index">{{item.name}}</view>
					<!--  #endif -->
					<!--  #ifdef APP-PLUS -->
					<view class="picker-item1" style="height: 36px;" v-for="(item, index) in (pickerList[twoIndex[0]] && pickerList[twoIndex[0]].children)" :key="index"><text v-html="item.name"></text></view>
					<!--  #endif -->
				</picker-view-column>
			</picker-view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "wzp-picker",
		props: {
			mode: {
				type: String,
				default: "one"
			},
			pickerList: {
				type: Array,
				default () {
					return []
				}
			}
		},
		data() {
			return {
				oneIndex: [0],
				twoIndex: [0, 0],
				pickerShow: false,
				defaultValue: [0],
			};
		},
		methods: {
			pickerViewChangeOne(e) {
				this.oneIndex = e.detail.value;
			},
			pickerViewChangeTwo(e) {
				this.twoIndex = e.detail.value;
			},
			showPicker(indexArray) {
				this.defaultValue = indexArray || [0];
				if(this.mode=='one') {
					this.oneIndex = this.defaultValue || [0];
				}else {
					this.twoIndex = this.defaultValue || [0, 0];
				}
				console.log(this.defaultValue,"组件中的this.defaultValue");
				setTimeout(()=> {
					this.pickerShow = true;
				}, 0);
			},
			confirm() {
				this.pickerShow = false;
				let indexArray = [];
				if (this.mode == "one") {
					indexArray = this.oneIndex;
				} else if (this.mode == "two") {
					indexArray = this.twoIndex;
				}
				let checkItems = [];
				this.checkItmes(this.pickerList, indexArray, checkItems, 0);
				this.$emit("onConfirm", {index: indexArray, value: checkItems});
			},
			closePicker() {
				this.pickerShow = false;
				this.$emit("onCancle");
			},
			checkItmes(data, indexArray, checkItems, index) {
				let arrItem = indexArray[index];
				let item = data[arrItem];
				if(indexArray.length == (index+1)) {
					checkItems.push(item);
					return true;
				}else if(item != null && Array.isArray(item.children) && item.children.length != 0) {
					let isPush = this.checkItmes(item.children, indexArray, checkItems, index+1);
					if(isPush) checkItems.push(item);
					return true;
				}
			}
		}
	}
</script>

<style>
	.picker-mask {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		z-index: 50;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.picker-content {
		flex-direction: column;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 100;
		width: 100%;
		height: 600upx;
		background-color: #FFFFFF;
		transform: translateY(100%);
		transition: all 200ms ease;
	}

	.pickerShow {
		transform: translateY(0) !important;
	}

	.picker-content .picker-button {
		justify-content: space-between;
		display: flex;
		height: 80upx;
		line-height: 80upx;
		padding: 0 30upx;
	}

	.picker-button text {
		font-size: 32upx;
		font-weight: 500;
	}

	.picker-button text:first-child {
		color: #A1A1A1;
	}

	.picker-button text:last-child {
		color: #1AAD19;
	}

	.picker-content .picker-view {
		width: 100%;
		height: 500upx;
	}

	.picker-content .picker-view-selected-one,
	.picker-content .picker-view-selected-two {
		/* height: 110upx;
		line-height: 110upx; */
		border-top: #1AAD19 1upx solid;
		border-bottom: #1AAD19 1upx solid;
	}

	.picker-content .picker-view-selected-one {
		position: relative;
		left: 25%;
		width: 50%;
	}

	.picker-content .picker-view-selected-two {
		position: relative;
		left: 15%;
		width: 70%;
	}

	.picker-view .picker-item1 {
		width: 100%;
		font-size: 32upx;
		font-weight: 600;
		display: flex;
		align-items: center;
		justify-content: center;
		white-space:nowap;
	}
</style>
